//
//  GPHomeView.m
//  GoldPig
//
//  Created by 虎爷 on 2019/10/17.
//  Copyright © 2019 虎爷. All rights reserved.
//

#import "GPHomeView.h"
#import "XBPraiseAnimation.h"
@interface GPHomeView ()
@property (weak, nonatomic) IBOutlet UIImageView *pigBgImg;
@property (weak, nonatomic) IBOutlet UILabel *levelLab;
@property (weak, nonatomic) IBOutlet UILabel *level2Lab;
@property (weak, nonatomic) IBOutlet UIView *timeDView;
@property (weak, nonatomic) IBOutlet UIView *levelGetView;
@property (weak, nonatomic) IBOutlet UIButton *renwuBtn;
@property (weak, nonatomic) IBOutlet UIButton *workBtn;
@property (nonatomic ,strong) XBPraiseAnimation *praiseAnimation;
@property (assign, nonatomic) SystemSoundID bloomSound;
@end
@implementation GPHomeView
- (void)awakeFromNib {
    [super awakeFromNib];
    [self configureSounds];
    
    [self setPraiseAni];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(makeShakeAnimation) userInfo:nil repeats:YES];
}

- (void)configureSounds
{
    // Configure bloom sound
    //
    NSString *bloomSoundPath = [[NSBundle mainBundle]pathForResource:@"bloom" ofType:@"wav"];
    NSURL *bloomSoundURL = [NSURL fileURLWithPath:bloomSoundPath];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)bloomSoundURL, &_bloomSound);
}


#pragma mark 动画
//抖动动画
-(void)makeShakeAnimation{
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    NSValue *value_0 = [NSNumber numberWithFloat:-M_PI/180*4];
    NSValue *value_1 = [NSNumber numberWithFloat:M_PI/180*4];
    NSValue *value_3 = [NSNumber numberWithFloat:-M_PI/180*4];
    anima.values = @[value_0,value_1,value_3];
    anima.repeatCount = 10;
    [self.timeDView.layer addAnimation:anima forKey:@"shakeAnimation"];
    [self.levelGetView.layer addAnimation:anima forKey:@"shakeAnimation"];
    [self.renwuBtn.layer addAnimation:anima forKey:@"shakeAnimation"];
    [self.workBtn.layer addAnimation:anima forKey:@"shakeAnimation"];
}

//飘出爱心
-(void)setPraiseAni{
    //设置点赞图片数组
    NSArray <UIImage *>* imageArray = @[[UIImage imageNamed:@"praise_sprite_0"]];
    
    //初始化点赞动画对象
    self.praiseAnimation = [[XBPraiseAnimation alloc] initWithImageArray:imageArray onView:self point:CGPointMake(25, 260)];
    
    //调节左右移动边距
    self.praiseAnimation.leftSpacing = 10;
    self.praiseAnimation.rightSpacing = 40;
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(praiseAnimationTimerHeadle) userInfo:nil repeats:YES];
}

-(void)praiseAnimationTimerHeadle{
    [self.praiseAnimation animate:2];
}

- (void)setLevel:(NSNumber *)level{
    _level = level;
    self.pigBgImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",level]];
    self.levelLab.text = [NSString stringWithFormat:@"%@级",level];
    self.level2Lab.text = [NSString stringWithFormat:@"Lv.%@",level];
}

- (IBAction)rainClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.rainClickBlock();
}
- (IBAction)taskClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.taskClickBlock();
}

- (IBAction)redClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.redClickBlock();
}
- (IBAction)diamondClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.diamondClickBlock();
}
- (IBAction)withdrawalCilck:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.withdrawalCilckBlock();
}
- (IBAction)mapClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.mapCilckBlock();
}
- (IBAction)getPigGoldClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.getPigGoldCilckBlock();
}

+ (instancetype)initGPHomeView{
    return NSbunleloadNibName(@"GPHomeView");
}

- (IBAction)checkClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.checkCilckBlock();
}

- (IBAction)workClick:(id)sender {
    AudioServicesPlaySystemSound(self.bloomSound);
    self.workCilckBlock();
}

@end
